package com.example.love4ever.photo;

import com.example.love4ever.dao.UserMapper;
import com.example.love4ever.model.Msg;
import com.example.love4ever.model.ResultModel;
import com.example.love4ever.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author zx
 * @Date 2020/5/25
 * @Version 1.0
 */
@RestController
@RequestMapping("/photo")
public class PhotoHandler {

    @Autowired
    UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/insertMsg")
    public ResultModel insertMsg(@RequestBody Msg param) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Msg msg = new Msg();
        msg.setName(param.getName());
        msg.setToName(param.getToName());
        msg.setPrivateFlag(param.getPrivateFlag());
        msg.setMsg(param.getMsg());
        msg.setTime(df.format(new Date()));
        userMapper.insertMsg(msg);
        return new ResultModel("1", "succ");
    }

    @ResponseBody
    @RequestMapping("/selectMsg")
    public ResultModel selectMsg(@RequestBody Msg param) {
        ResultModel resultModel = new ResultModel();
        Msg msg = new Msg();
        msg.setId(param.getId());
        msg.setName(param.getName());
        msg.setToName(param.getToName());
        List<User> users = userMapper.selectInfo(msg);
        User user = users.get(0);
        if (!param.getName().equals(user.getName())) {
            msg.setPrivateFlag("0");
        } else {
            user.setNameFlag("1");
        }
        msg.setName(user.getName());
        List<Msg> msgs = userMapper.selectMsg(msg);
        resultModel.setUser(user);
        resultModel.setMsgList(msgs);
        resultModel.setMsg("succ");
        resultModel.setCode("1");
        return resultModel;
    }

    @ResponseBody
    @RequestMapping("/insertInfo")
    public ResultModel insertInfo(@RequestBody User param) {
        User user = new User();
        BeanUtils.copyProperties(param, user);
        userMapper.insertInfo(user);
        return new ResultModel("1", "succ");
    }

    @ResponseBody
    @RequestMapping("/deleteMsg")
    public ResultModel deleteMsg(@RequestBody Msg param) {
        Msg msg = new Msg();
        BeanUtils.copyProperties(param, msg);
        userMapper.deleteMsg(msg);
        return new ResultModel("1", "succ");
    }

    @ResponseBody
    @RequestMapping("/updatePwd")
    public ResultModel updatePwd(@RequestBody User param) {
        User user = new User();
        User userOld = new User();
        BeanUtils.copyProperties(param, user);
        userOld.setShortName(param.getName());
        userOld.setPassword(param.getOldpassword());
        List<User> login = userMapper.login(userOld);
        if (login.size() > 0) {
            int i = userMapper.updateInfo(user);
            if (i > 0) {
                return new ResultModel("1", "succ");
            }
        }
        return new ResultModel("0", "succ");
    }

}
